#include<iostream>
#include<Windows.h>
#include <DbgHelp.h>
#include <tchar.h>
using namespace std;

#pragma comment(lib, "dbghelp.lib") 

int main() 
{ 
	  char cBuff[1024] = {0};  
    unsigned int   i;  
    void         * stack[128];  
    unsigned short frames;  
    SYMBOL_INFO  * symbol;  
    HANDLE         process;  
  
    process = GetCurrentProcess();  
  
    SymInitialize(process, NULL, TRUE);  
  
    frames = CaptureStackBackTrace(0, 128, stack, NULL);  
    symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);  
    symbol->MaxNameLen = 255;  
    symbol->SizeOfStruct = sizeof(SYMBOL_INFO);  
  
    OutputDebugString(_T("##########################################################################\n"));  
    for (i = 0; i < frames; i++)  
    {  
        SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol);  
        sprintf_s(cBuff,1024,("%i: %s - 0x%0X\n"),frames - i - 1, symbol->Name, symbol->Address);  
        OutputDebugStringA(cBuff);

		IMAGEHLP_LINE lineInfo = { sizeof(IMAGEHLP_LINE) };
		DWORD dwLineDisplacement;

		printf("%s\t",cBuff);
		if( SymGetLineFromAddr( process, (DWORD64)(stack[i]), &dwLineDisplacement, &lineInfo ) )
		{
			
		printf( "[Source File : %s]\t", lineInfo.FileName ); 
		printf( "[Source Line : %u]\n", lineInfo.LineNumber ); 
		}
		else
		{
		printf( "SymGetLineFromAddr failed!\n" );
		}
    }  
    OutputDebugString(_T("--------------------------------------------------------------------\n"));  
  
    free(symbol);  
  return 0; 
} 
